package com.t114514.demo.mapper;
import com.t114514.demo.pojo.*;
import com.t114514.demo.pojo.Options;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionnaireMapper {

    @Insert("INSERT INTO questionnaire (questionnaireTitle,questionnaireStatus) VALUES (#{questionnaireTitle},2 )")
    void create_empty(String questionnaireTitle);

    @Select("select * from questionnaire")
    List<Questionnaire> getAllQuestionnaire();

    //获得questionnaireId的问卷的问题数量
    @Select("SELECT count(*) FROM  question where questionnaireId=#{questionnaireId}")
    int getQuestionNumber(int questionnaireId);

    @Insert("INSERT INTO question (questionNumber,questionType,questionContent,required,questionnaireId) "+
            "Values(#{questionNumber},#{questionType},#{questionContent},#{required},#{questionnaireId})")
    void addQuestion(Question question);

    @Delete(" DELETE FROM question WHERE questionId = #{questionId}")
    void deleteQuestion(Integer questionId);

    @Select("select * from question where questionId=#{questionId}")
    Question getQuestion(Integer questionId);

    @Update("UPDATE question SET questionNumber=questionNumber-1 where questionNumber>#{questionNumber}")
    void updateQuestionNumber(Integer questionNumber);

    @Select("SELECT count(*) FROM options where questionId=#{questionId}")
    Integer getOptionNumber(Integer questionId);

    @Insert("INSERT INTO options (optionNumber,optionContent,optionValue,questionId)" +
            "VALUES (#{optionNumber},#{optionContent},#{optionValue},#{questionId})")
    void addOption(Options option);

    @Select("select * from options where optionId=#{optionId}")
    Options getOption(Integer optionId);

    @Update("UPDATE options SET optionNumber=optionNumber-1 where optionNumber>#{optionNumber}")
    void updateOptionNumber(int optionNumber);

    @Delete("delete from options where optionId=#{optionId}")
    void deleteOption(Integer optionId);

    @Select("select * from question where questionnaireId=#{questionnaireId}")
    List<Question> getAllQuestion(Integer questionnaireId);

    @Select("select * from options where questionId=#{questionId}")
    List<Options> getAllOption(Integer questionId);

    @Select("select * from question where questionnaireId=#{questionnaireId} and questionType = 1 order by questionNumber asc")
    List<Question> getQuestionByquestionnaireId(int questionnaireId);

    @Select("select * from options where questionId=#{questionId}")
    List<Options> selectOptionsByQuestionId(Integer questionId);

    @Select("select * from question where questionnaireId=#{questionnaireId} and questionType = 2 order by questionNumber asc")
    List<Question> selectQuestionByquestionnaireId(int questionnaireId);
    @Delete("delete from options where questionId=#{questionId}")
    void deleteAllOption(Integer questionId);

    @Update("update questionnaire set questionnaireStatus=1 where questionnaireId=#{questionnaireId}")
    void submitQuestionnaire(Questionnaire questionnaire);

    @Select("SELECT q.questionId " +
            "FROM question q " +
            "LEFT JOIN options o ON q.questionId = o.questionId " +
            "WHERE q.questionnaireId = #{questionnaireId} AND q.questionType = 1 " +
            "GROUP BY q.questionId " +
            "HAVING COUNT(o.optionId) < 2")
    List<Integer> checkQuestionnaire(Questionnaire questionnaire);

    @Select("select studentId,teacherId from teacher_course_student")
    List<TeacherAndStudent> getAllTeacherAndStudent();

    @Insert("insert into student_questionnaire_teacher (questionnaireId,studentId,teacherId,isValidate) " +
            "values (#{questionnaireId},#{studentId},#{teacherId},0)")
    void publishQuestionnaire(int questionnaireId, int studentId, int teacherId);
    @Select("select * from questionnaire where teacherId=#{teacherId}")
    List<Questionnaire> getQuestionnaire(int teacherId);
}
